package com.molichuxing.services.infrastructure.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.services.infrastructure.bean.OrderSubscribeSettlementBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OrderSubscribeSettlementMapper;
import com.molichuxing.services.infrastructure.dto.request.create.OrderSubscribeSettlementCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderPayModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderSubscribeSettlementModifyDto;
import com.molichuxing.services.infrastructure.dto.response.OrderSubscribeSettlementDto;
import com.molichuxing.services.infrastructure.service.OrderSubscribeSettlementService;
import com.molichuxing.services.property.OrderSubscribeSettlementPayStatusEnum;

/**
 * <p>
 * 订阅订单结算表 服务实现类
 * </p>
 *
 * @author renzhengyu
 * @since 2019-08-12
 */
@Service("orderSubscribeSettlementService")
public class OrderSubscribeSettlementServiceImpl implements OrderSubscribeSettlementService {
	private static final Logger logger = LoggerFactory.getLogger(OrderSubscribeSettlementServiceImpl.class);

	@Resource
	private OrderSubscribeSettlementMapper orderSubscribeSettlementMapper;

	/**
	 * 获取订阅里程订单结算信息
	 * 
	 * @param orderCode
	 *            订单号
	 * @return
	 */
	@Override
	public OrderSubscribeSettlementDto getOrderSubscribeSettlement(Long orderCode) {
		return getOrderSubscribeSettlement(orderCode, null);
	}

	/**
	 * 获取订阅里程订单结算信息
	 * 
	 * @param orderCode
	 *            订单号
	 * @param status
	 *            支付状态
	 * @return
	 */
	@Override
	public OrderSubscribeSettlementDto getOrderSubscribeSettlement(Long orderCode,
			OrderSubscribeSettlementPayStatusEnum status) {
		return orderCode == null ? null
				: Convert.toOrderSubscribeSettlementDto(orderSubscribeSettlementMapper
						.getOrderSubscribeSettlement(orderCode, status == null ? null : status.getValue()));
	}

	/**
	 * 支付完成修改结算信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean modifyPay(OrderPayModifyDto modifyDto) {
		if (orderSubscribeSettlementMapper.modifyPay(Convert.toOrderSubscribeSettlementBean(modifyDto),
				OrderSubscribeSettlementPayStatusEnum.WAIT.getValue()) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 新增
	 * 
	 * @param createDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean create(OrderSubscribeSettlementCreateDto createDto) {
		orderSubscribeSettlementMapper.create(Convert.toOrderSubscribeSettlementBean(createDto));
		return true;
	}

	/**
	 * pdi修改结算信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean modifyPdi(OrderSubscribeSettlementModifyDto modifyDto) {
		if (orderSubscribeSettlementMapper.modifyPdi(Convert.toOrderSubscribeSettlementBean(modifyDto),
				OrderSubscribeSettlementPayStatusEnum.WAIT.getValue()) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 无费用结算修改
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean modifySettlement(Long orderCode) {
		OrderSubscribeSettlementBean bean = orderSubscribeSettlementMapper
				.getOrderSubscribeSettlement(orderCode, null);
		if (bean == null) {
			logger.error("[modifySettlement]订单({})结算信息不存在", orderCode);
			return false;
		} else if (bean.getTotalFee().compareTo(BigDecimal.ZERO) != 0) {
			logger.error("[modifySettlement]订单({})结算金额不为0，不能无费用结算", orderCode);
			return false;
		} else if (OrderSubscribeSettlementPayStatusEnum.WAIT.getValue() != bean.getStatus()) {
			logger.error("[modifySettlement]订单({})结算状态不为未结算", orderCode);
			return false;
		}
		bean.setStatus(OrderSubscribeSettlementPayStatusEnum.FREE_SETTLE.getValue());
		if (orderSubscribeSettlementMapper.modifySettlement(bean,
				OrderSubscribeSettlementPayStatusEnum.WAIT.getValue()) > 0) {
			return true;
		}

		return false;
	}

	/**
	 * 争议处理修改结算信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean modifyDisputeDisposals(OrderSubscribeSettlementModifyDto modifyDto) {
		if (orderSubscribeSettlementMapper.modifyDisputeDisposals(Convert.toOrderSubscribeSettlementBean(modifyDto),
				OrderSubscribeSettlementPayStatusEnum.WAIT.getValue()) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 查询订单结算信息
	 * 
	 * @param orderCodes
	 *            订单号
	 * @return
	 */
	@Override
	public Map<Long, OrderSubscribeSettlementDto> getOrderSubscribeSettlement(List<Long> orderCodes) {
		List<OrderSubscribeSettlementBean> beans = orderSubscribeSettlementMapper
				.getOrderSubscribeSettlementList(orderCodes);

		if (beans == null || beans.isEmpty()) {
			return null;
		}

		return beans.stream().collect(
				Collectors.toMap(OrderSubscribeSettlementBean::getOrderCode,
						bean -> Convert.toOrderSubscribeSettlementDto(bean)));
	}
}
